﻿using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WWLauncher.Constant;
using WWLauncher.Libs.NPinyin;
using WWLauncher.View.Other;
using WWLauncher.ViewModel;

namespace WWLauncher
{
    public partial class MainWindow
    {
        /// <summary>
        /// 搜索快捷键按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchHotKeyDown(object sender, CanExecuteRoutedEventArgs e)
        {
            if (Config.SearchType == SearchType.HotKey && !RunTimeStatus.SearchBoxShow)
            {
                ShowSearchBox();
            }
            else if (RunTimeStatus.SearchBoxShow)
            {
                HidedSearchBox();
            }
        }
        /// <summary>
        /// 搜索按钮按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            SearchHotKeyDown(null, null);
        }
        /// <summary>
        /// 显示搜索框
        /// </summary>
        private void ShowSearchBox()
        {
            RunTimeStatus.SearchBoxShow = true;
            RightCard.VisibilitySearchCard(Visibility.Visible);
            SearchBox.Width = 360;
            SearchBox.Focus();

            //执行一遍a查询
            //SearchBox_TextChanged(null, null);
        }
        private void SearchBox_SearchStarted(object sender, HandyControl.Data.FunctionEventArgs<string> e)
        {
            if (!RunTimeStatus.SearchBoxShow)
                ShowSearchBox();

            //刷新搜索后 鼠标移动次数置为0
            RunTimeStatus.MouseMoveCount = 0;
            //隐藏popup
            RightCard.MyPoptip.IsOpen = false;

            if (!string.IsNullOrEmpty(e.Info))
            {
                //显示搜索结果列表
                RightCard.VisibilitySearchCard(Visibility.Visible);
                //暂时隐藏条目信息
                SearchResContainer.Visibility = Visibility.Collapsed;
                //显示加载条
                RightCard.LoadingRightCard.Visibility = Visibility.Visible;
                object obj = RightCard.VerticalCard.Content;
                if (obj != null)
                {
                    SearchResControl control = obj as SearchResControl;
                    control.VerticalUFG.Visibility = Visibility.Collapsed;
                }
                SearchDelay();
            }
            else
            {
                //隐藏条目信息
                SearchResContainer.Visibility = Visibility.Collapsed;
                //清空查询结果
                object obj = RightCard.VerticalCard.Content;
                if (obj != null)
                {
                    SearchResControl control = obj as SearchResControl;
                    control.VerticalUFG.Visibility = Visibility.Collapsed;
                }
            }

        }

        private void SearchDelay()
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                string inputText = SearchBox.Text.ToLower().Trim();
                if (string.IsNullOrEmpty(inputText))
                {
                    RightCard.LoadingRightCard.Visibility = Visibility.Collapsed;
                    return;
                }
                Task.Run(() =>
                {
                    var resList = new ObservableCollection<IconInfo>();

                    //WWLauncher数据搜索
                    var menuList = ViewModel.MenuList;
                    foreach (MenuViewModel menu in menuList)
                    {
                        var iconList = menu.IconList;
                        foreach (IconInfo icon in iconList)
                        {
                            string pyName = Pinyin.GetInitials(icon.Name).ToLower();
                            if (icon.Name.Contains(inputText) || pyName.Contains(inputText))
                            {
                                resList.Add(icon);
                            }
                        }
                    }

                    this.Dispatcher.BeginInvoke(() =>
                    {
                        SearchResControl control = new SearchResControl(resList, this)
                        {
                            RightCard = this.RightCard
                        };
                        RightCard.VerticalCard.Content = control;
                        //关闭加载效果
                        RightCard.LoadingRightCard.Visibility = Visibility.Collapsed;
                    });
                });
            });
        }

        /// <summary>
        /// 隐藏搜索框
        /// </summary>
        public void HidedSearchBox()
        {
            RunTimeStatus.SearchBoxHided300 = false;
            new Thread(() =>
            {
                Thread.Sleep(300);
                RunTimeStatus.SearchBoxHided300 = true;
            }).Start();
            new Thread(() =>
            {
                Thread.Sleep(1000);
            }).Start();
            new Thread(() =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    Keyboard.Focus(SearchBox);
                    RunTimeStatus.SearchBoxShow = false;
                    //SearchBox.TextChanged -= SearchBox_TextChanged;
                    //SearchBox.Clear();
                    //SearchBox.TextChanged += SearchBox_TextChanged;
                    SearchBox.Width = 0;
                    SearchResContainer.Visibility = Visibility.Collapsed;
                    RightCard.VerticalCard.Content = null;
                    RightCard.VisibilitySearchCard(Visibility.Collapsed);
                });
            }).Start();
        }
    }
}
